﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using QLKS.Templates;

namespace QLKS.Views
{
    /// <summary>
    /// Interaction logic for frmThemPhong.xaml
    /// </summary>
    public partial class frmThemPhong : FormTrans
    {

        public bool UpdateSucces = false;
        Command cm;
        DataTable Data = new DataTable();

        public bool IsEditMode
        {
            get { return (bool)GetValue(IsEditModeProperty); }
            set { SetValue(IsEditModeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsEditMode.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEditModeProperty =
            DependencyProperty.Register("IsEditMode", typeof(bool), typeof(frmThemPhong), new UIPropertyMetadata(true));

        public frmThemPhong()
        {
            InitializeComponent();
            IsEditMode = true;

        }

        public frmThemPhong(DataTable tb, Command _cm)
        {
            InitializeComponent();
            cbbLoaiPhong.ItemsSource = ViewModels.PhongViewModel.GetLoaiPhong().DefaultView;
            cbbTinhTrang.ItemsSource = ViewModels.PhongViewModel.GetTinhTrangPhong().DefaultView;
            cbbTang.ItemsSource = ViewModels.PhongViewModel.GetTang().DefaultView;

            IsEditMode = _cm == Command.View ? false : true;
            cm = _cm;
            if (tb == null)
                Data = ViewModels.PhongViewModel.CreateNewPhong();
            else
                Data = tb.Copy();
            gridMain.DataContext = Data.DefaultView;
           
        }

        //CheckValid
        private bool KiemTra()
        {
            
                if (txtSucChua.Value == null || int.Parse(txtSucChua.Value.ToString()) == 0)
                {
                    MessageBox.Show(M_LAN.Equals("V")?"Chưa vào sức chứa":"please input capacity", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtSucChua.Focus();
                    return false;
                }
                else
                    if (ViewModels.PhongViewModel.KiemTraPhongTonTai(txtMaPhong.Text) && cm == Command.New)
                    {
                        MessageBox.Show(M_LAN.Equals("V")?"Mã phòng đã tồn tại!":"room id has existed", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                        txtMaPhong.Focus();
                        return false;
                    }
                    else
                        if (ViewModels.PhongViewModel.KiemTraViTriKhongHopLe(int.Parse(cbbTang.Text), int.Parse(txtVitri.Text)) && cm == Command.New)
                        {
                            MessageBox.Show(M_LAN.Equals("V")?"Vị trí này đã có phòng!":"this location has existed room", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                            //cbbVitri.Focus();
                            return false;
                        }

            return true;
        }

        //Save
        void tbMain_Cm_Save()
        {
            //Thêm vào CSDL dữ liệu
            
            if (KiemTra())
            {
                if (ViewModels.PhongViewModel.ThemPhong(Data))
                {
                    this.UpdateSucces = true;
                    if (cm == Command.Edit)
                        MessageBox.Show(M_LAN.Equals("V")?"Cập nhật phòng thành công!":"Update room successful", "HotelManager", MessageBoxButton.OK, MessageBoxImage.Information);
                    else
                    {
                        MessageBox.Show(M_LAN.Equals("V")?"Thêm phòng thành công!":"Insert room successful", "HotelManager", MessageBoxButton.OK, MessageBoxImage.Information);
                        cm = Command.Edit;  
                    }
                    //Close();
                }
            }
        }

        //Thoát
        void tbMain_Cm_Exit()
        {
            this.Close();
        }

        private void tbMain_Cm_New()
        {
            Data = ViewModels.PhongViewModel.CreateNewPhong();
            gridMain.DataContext = Data.DefaultView;
            cm = Command.New;
            cbbLoaiPhong.Focus();
        }

        private void FormTrans_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable tb = ViewModels.TienTeViewModel.GetDmTienTe();
            if (IsEditMode)
            {
                tbMain.Cm_Save += new ucXToolBar.Execute(tbMain_Cm_Save);
                //if(cm == Command.New)
                    tbMain.Cm_New += new ucXToolBar.Execute(tbMain_Cm_New);
            }
            //txtMaPhong.IsReadOnly = !(cm == Command.New || cm == Command.Copy);
            cbbLoaiPhong.Focus();
            tbMain.Cm_Exit += new ucXToolBar.Execute(tbMain_Cm_Exit);
            txtMaPhong.Text = ViewModels.PhongViewModel.GetMaPhong(int.Parse(cbbTang.Text), int.Parse(txtVitri.Text));
            txtMaPhong.IsReadOnly = true;
            cbbTang.IsEnabled = cm == Command.New ? true : false;
        }

        private void cbbTang_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((sender as ComboBox).SelectedValue != null)
            {
                int tang = int.Parse((sender as ComboBox).SelectedValue.ToString());
                Data.Rows[0]["vitri"] = ViewModels.PhongViewModel.GetViTriPhongMax(tang) + 1;
                Data.Rows[0]["maphong"] = ViewModels.PhongViewModel.GetMaPhong(tang, int.Parse(txtVitri.Text));
            }
        }
        
        //Lấy giá tiền theo loại phòng
        private void cbbLoaiPhong_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = (sender as ComboBox);
            if (cb.SelectedValue == null)
                return;
            Data.Rows[0]["giatheogio"] = ViewModels.PhongViewModel.GetGiaTienTheoGioVND(cb.SelectedValue.ToString());
            Data.Rows[0]["giatheongay"] = ViewModels.PhongViewModel.GetGiaTienTheoNgayVND(cb.SelectedValue.ToString());
        }

        private void cbbLoaiPhong_GotFocus(object sender, RoutedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            if (cb != null)
                cb.IsDropDownOpen = true;
        }

        private void txtGhiChu_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            Data.Rows[0]["ghichu"] = txtGhiChu.Text;
        }

    }
}
